package com.example.tupiancs34;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class ImageShareHelper {

    private static final String AUTHORITY = "com.example.tupiancs34.provider";
    private static final String BASE_PATH = "images";

    /**
     * 分享单张图片给其他应用
     */
    public static void shareImage(Context context, String imageFileName) {
        Uri imageUri = Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH + "/" + imageFileName);
        
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("image/*");
        shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        
        Intent chooser = Intent.createChooser(shareIntent, "分享图片");
        if (chooser.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(chooser);
        } else {
            Toast.makeText(context, "没有找到可以分享的应用", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 分享多张图片给其他应用
     */
    public static void shareMultipleImages(Context context, List<String> imageFileNames) {
        ArrayList<Uri> imageUris = new ArrayList<>();
        
        for (String fileName : imageFileNames) {
            Uri imageUri = Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH + "/" + fileName);
            imageUris.add(imageUri);
        }
        
        Intent shareIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
        shareIntent.setType("image/*");
        shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        
        Intent chooser = Intent.createChooser(shareIntent, "分享多张图片");
        if (chooser.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(chooser);
        } else {
            Toast.makeText(context, "没有找到可以分享的应用", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 获取图片的Content URI
     */
    public static Uri getImageUri(String imageFileName) {
        return Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH + "/" + imageFileName);
    }

    /**
     * 获取所有图片的基础URI
     */
    public static Uri getBaseUri() {
        return Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH);
    }
}
